package com.wanmao.test.adapter;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


import com.wanmao.test.R;
import com.wanmao.test.Test_XiZeActivity;
import com.wanmao.test.bean.XZBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<XZBean> list;

    private static HashMap<Integer, Boolean> isSelected;

    private Context context;

    private LayoutInflater inflater = null;

    public String RWID;

    public String getRWID() {
        return RWID;
    }

    public void setRWID(String RWID) {
        this.RWID = RWID;
    }


    public MyAdapter(List<XZBean> list, Context context) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        isSelected = new HashMap<Integer, Boolean>();

        initDate();
    }


    private void initDate() {
        for (int i = 0; i < list.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {

            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item, null);
            holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
            holder.tv_xm = (TextView) convertView.findViewById(R.id.tv_xize_xm);
            holder.tv_yd = (TextView) convertView.findViewById(R.id.tv_xize_yd);
            holder.tv_ywc = (TextView) convertView.findViewById(R.id.tv_xize_ywc);
            holder.tv_wwc = (TextView) convertView.findViewById(R.id.tv_xize_wwc);
            holder.tv_hg = (TextView) convertView.findViewById(R.id.tv_xize_hg);
            holder.tv_bhg = (TextView) convertView.findViewById(R.id.tv_xize_bhg);
            holder.tv_bsj = (TextView) convertView.findViewById(R.id.tv_xize_bsj);
            holder.tv_wjc = (TextView) convertView.findViewById(R.id.tv_xize_wjc);
            ;
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_xize_number);
            holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }


        int numb = 0;
        SharedPreferences sp = context.getSharedPreferences(list.get(position).getXZ_ID()+RWID, context.MODE_PRIVATE);
        numb = sp.getInt("end_number", numb);
        Test_XiZeActivity.str = numb + "/"+list.get(position).getXMSIZE();


        holder.tv_number.setText(numb + "/" + list.get(position).getXMSIZE());
        holder.tv_xm.setText("项目：\n" + list.get(position).getXMSIZE());
        holder.tv_yd.setText("要点：\n" + list.get(position).getYDSIZE());
        holder.tv_ywc.setText("已完成：\n" + numb);
        int size= Integer.parseInt(list.get(position).getXMSIZE());
        int i=size-numb;
        holder.tv_wwc.setText("未完成：\n"+i);
        int hege;
        int buhege;
        int busheji;
        if ("".equals(list.get(position).getHEGE())){
            hege= 0;
            holder.tv_hg.setText("合格：\n" +"0" );
        }else {
            holder.tv_hg.setText("合格：\n" +list.get(position).getHEGE() );
            hege=Integer.parseInt(list.get(position).getHEGE());
        }
        if ("".equals(list.get(position).getBUHEGE())){
            buhege=0;
            holder.tv_bhg.setText("不合格：\n" + "0");
        }else {
            holder.tv_bhg.setText("不合格：\n" + list.get(position).getBUHEGE());
            buhege=Integer.parseInt(list.get(position).getBUHEGE());
        }
       if ("".equals(list.get(position).getBUSHEJI())){
           holder.tv_bsj.setText("不涉及：\n" + "0");
           busheji=0;
       }else {
           holder.tv_bsj.setText("不涉及：\n" + list.get(position).getBUSHEJI());
           busheji=Integer.parseInt(list.get(position).getBUSHEJI());
       }


        int yd= Integer.parseInt(list.get(position).getYDSIZE());
        int wjc=yd-hege-buhege-busheji;
        holder.tv_wjc.setText("未检查：\n" + wjc);

        holder.tv.setText(list.get(position).getXZMC());
        holder.cb.setChecked(true);
        if (Test_XiZeActivity.editMode) {
            holder.cb.setVisibility(View.VISIBLE);
        } else {
            holder.cb.setVisibility(View.GONE);
        }
        holder.cb.setChecked(getIsSelected().get(position));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        MyAdapter.isSelected = isSelected;
    }

    public class ViewHolder {
        TextView tv, tv_xm, tv_yd, tv_ywc, tv_wwc, tv_hg, tv_bhg, tv_bsj, tv_wjc;
        TextView tv_number;
        public CheckBox cb;
    }

}